package com.guigu.pd.work.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.common.utils.PageVo;
import com.guigu.pd.serviceapi.authfeign.CoreOrgFeign;
import com.guigu.pd.serviceapi.authfeign.UserFeign;
import com.guigu.pd.serviceapi.basefeign.TripsTruckDriverFeign;
import com.guigu.pd.serviceapi.basefeign.TruckFeign;
import com.guigu.pd.serviceapi.omsfeign.OrderCargoFeign;
import com.guigu.pd.serviceapi.pojo.OrderCargoEntity;
import com.guigu.pd.serviceapi.workfeign.TransportOrderTaskFeign;
import com.guigu.pd.work.entity.DriverJobEntity;
import com.guigu.pd.work.entity.TaskTransportEntity;
import com.guigu.pd.work.entity.TransportOrderEntity;
import com.guigu.pd.work.service.TransportOrderService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.common.utils.PageUtils;
import com.guigu.common.utils.Query;

import com.guigu.pd.work.dao.TaskTransportDao;
import com.guigu.pd.work.entity.TaskTransportEntity;
import com.guigu.pd.work.service.TaskTransportService;


@Service("taskTransportService")
public class TaskTransportServiceImpl extends ServiceImpl<TaskTransportDao, TaskTransportEntity> implements TaskTransportService {

    @Autowired
    CoreOrgFeign coreOrgFeign;

    @Autowired
    TripsTruckDriverFeign tripsTruckDriverFeign;

    @Autowired
    UserFeign userFeign;

    @Autowired
    TruckFeign truckFeign;

    @Autowired
    TransportOrderTaskFeign transportOrderTaskFeign;

    @Autowired
    TransportOrderService transportOrderService;

    @Autowired
    OrderCargoFeign orderCargoFeign;

    @Override
    public PageVo<TaskTransportEntity> queryPage(Map<String, Object> params) {
        TaskTransportEntity
                taskTransport = JSON.parseObject(JSON.toJSONString(params),
                TaskTransportEntity.class);
        Integer pageno = Integer.parseInt(params.get("pageno").toString());
        Integer pagesize = Integer.parseInt(params.get("pagesize").toString());

        LambdaQueryWrapper<TaskTransportEntity> queryWrapper = new LambdaQueryWrapper<TaskTransportEntity>();
        queryWrapper.like(StringUtils.isNotEmpty(taskTransport.getId()),
                TaskTransportEntity::getId, taskTransport.getId());

        queryWrapper.like(taskTransport.getStatus() != null && taskTransport.getStatus() != 0,
                TaskTransportEntity::getStatus, taskTransport.getStatus());

        if (StringUtils.isNotEmpty(taskTransport.getDrivername())) {
            String uid = userFeign.getidbyName(taskTransport.getDrivername());
            if (uid != null && uid != "") {
                List<String> tripslist = tripsTruckDriverFeign.gettripsidbyuserid(uid);
                for (int i = 0; i < tripslist.size(); i++) {
                    queryWrapper.or().like(StringUtils.isNotEmpty(tripslist.get(i)),
                            TaskTransportEntity::getTransportTripsId,
                            tripslist.get(i));
                    this.list();
                }
            }
        }

        PageHelper.startPage(pageno, pagesize);

        List<TaskTransportEntity> tasktransportlist = this.list(queryWrapper);

        for (TaskTransportEntity transport : tasktransportlist) {
            String startagency = coreOrgFeign.getcornamebyid(transport.getStartAgencyId());
            String endagency = coreOrgFeign.getcornamebyid(transport.getEndAgencyId());
            transport.setStartagencyname(startagency);
            transport.setEndagencyname(endagency);

            String license = truckFeign.gettrucknamebyid(transport.getTruckId());
            transport.setLicense(license);

            String uid = tripsTruckDriverFeign.getuseridbytripsid(transport.getTransportTripsId());
            if (uid != null && uid != "") {
                String uname = userFeign.getuserinfobyId(uid);
                transport.setDrivername(uname);
            }

        }

        PageInfo<TaskTransportEntity> pageInfo = new PageInfo<TaskTransportEntity>(tasktransportlist);

        PageVo<TaskTransportEntity> pageVo = new PageVo<TaskTransportEntity>();
        pageVo.setRows(pageInfo.getList());
        pageVo.setTotal(pageInfo.getTotal());

        return pageVo;
    }

    @Override
    public TaskTransportEntity getinfoById(String id) {
        TaskTransportEntity tasktransport = this.getById(id);
        String startagency = coreOrgFeign.getcornamebyid(tasktransport.getStartAgencyId());
        String endagency = coreOrgFeign.getcornamebyid(tasktransport.getEndAgencyId());
        tasktransport.setStartagencyname(startagency);
        tasktransport.setEndagencyname(endagency);

        String license = truckFeign.gettrucknamebyid(tasktransport.getTruckId());
        tasktransport.setLicense(license);

        String uid = tripsTruckDriverFeign.getuseridbytripsid(tasktransport.getTransportTripsId());
        if (uid != null && uid != "") {
            String uname = userFeign.getuserinfobyId(uid);
            tasktransport.setDrivername(uname);
        }

        List<TransportOrderEntity> transportOrderlist = new ArrayList<TransportOrderEntity>();
        List<String> toidlist = transportOrderTaskFeign.gettransportorderIdbytaskId(tasktransport.getId());
        for (String toid : toidlist) {
            TransportOrderEntity transportOrder = transportOrderService.getById(toid);
            OrderCargoEntity orderCargo = orderCargoFeign.getordercargobyId(transportOrder.getOrderId());
            transportOrder.setQuantity(orderCargo.getQuantity());
            transportOrderlist.add(transportOrder);
        }

        tasktransport.setTransportOrder(transportOrderlist);


        return tasktransport;
    }

}